package com.wy.email.controller;

import com.wy.email.vo.EmailVo;
import freemarker.cache.ClassTemplateLoader;
import freemarker.cache.TemplateLoader;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.*;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.mail.internet.MimeMessage;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Objects;

/**
 * @author zzq
 * @date 2023/10/18
 */
@RestController
public class EmailController {


    @Autowired
    private JavaMailSender javaMailSender;

    // 获取yml配置的发送者邮箱
    @Value("${spring.mail.username}")
    private String mainUserName;

    //发送人昵称
    @Value("${spring.mail.nickname}")
    private String nickname;


    @PostMapping("send")
    public String sendEmail(@RequestBody EmailVo emailVo) throws IOException, TemplateException {
        String toMail = emailVo.getToMail();
        String title = emailVo.getTitle();
        String content = emailVo.getContent();


        freemarker.template.Configuration configuration = new freemarker.template.Configuration(freemarker.template.Configuration.VERSION_2_3_19);
        TemplateLoader templateLoader = new ClassTemplateLoader(this.getClass(), "/templates/");
        configuration.setTemplateLoader(templateLoader);
        Template template = configuration.getTemplate("mail.ftl");
        String html = FreeMarkerTemplateUtils.processTemplateIntoString(template, new HashMap<>());


        MimeMessage message = javaMailSender.createMimeMessage();
        InputStream is = null;
        try {
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom(nickname + '<' + mainUserName + '>');//发送者
            helper.setTo(toMail);//收件者

            if (title != null && !title.isEmpty()) {
                helper.setSubject(title);
            } else {
                helper.setSubject("文鳐AI大模型检测报告");//主题
            }

            if (content != null && !content.isEmpty()) {
                helper.setText(content);//内容
            } else {
                helper.setText(html, true);//内容
            }
            //  File file = ResourceUtils.getFile("static/111.pdf");


//            String path = Objects.requireNonNull(this.getClass().getResource("/111.pdf")).getPath();
//
//            File file = new File(path);


            ClassPathResource classPathResource = new ClassPathResource("111.pdf");
            is = classPathResource.getInputStream();
//            InputStreamResource s = new InputStreamResource(is);

            // File file = ResourceUtils.getFile("classpath:111.pdf");
            helper.addAttachment("目标检测报告.pdf", new ByteArrayResource(IOUtils.toByteArray(is)));//设置附件

            javaMailSender.send(message);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (is != null) {
                is.close();
            }
        }


        return "OK";
    }


}
